package com.youjiang.web;

import com.youjiang.dao.entity.Examination;
import com.youjiang.dao.entity.Result;
import com.youjiang.dao.entity.Subject;
import com.youjiang.model.Question;
import com.youjiang.model.Score;
import com.youjiang.service.IExamService;
import com.youjiang.service.IResultService;
import com.youjiang.service.ISubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * Created by yujia on 2017/11/16.
 */
@org.springframework.stereotype.Controller
@RequestMapping("/")
@Scope
public class Controller extends BaseController{
    @Autowired
    private ISubjectService subjectService;
    @Autowired
    private IExamService examService;
    @Autowired
    private IResultService resultService;

    @ResponseBody
    @RequestMapping("init/subject")
    public String initSubject() {
        System.out.println("请求接口:init/subject");
        List<String> subjects = subjectService.getLevel1Subjects();
        return gson.toJson(subjects);
    }

    @ResponseBody
    @RequestMapping("subjectL1/subjectL2")
    public String askSubjectL2ByL1(@RequestBody Subject subL1) throws UnsupportedEncodingException {
        String subname = subL1.getSubL1().toString();
//        subname = new String(subname.getBytes("ISO-8859-1"), "UTF-8");
        System.out.println("请求接口:subjectL1/subjectL2");
        List<String> subjects = subjectService.getLevel2Subjects(subname);
        return gson.toJson(subjects);
    }

    @ResponseBody
    @RequestMapping("test")
    public String askTest() throws UnsupportedEncodingException {
//        String subname = sub.getSubL2().toString();
//        subname = new String(subname.getBytes("ISO-8859-1"), "UTF-8");
        System.out.println("请求接口:test");
//        List<String> subjects = subjectService.getLevel2Subjects(subname);
        return "test";
    }

    @ResponseBody
    @RequestMapping("exam/questions.json") //加不加@RequestBody 都可以
    public String askQuestions(@RequestBody Question question) throws UnsupportedEncodingException {
        System.out.println("请求接口:exam/questions");
        List<Question> questions = examService.getExamQuestionsByClassify(question.getClassify());
//        List<String> subjects = subjectService.getLevel2Subjects(subname);
        return gson.toJson(questions);
    }


    @ResponseBody
    @RequestMapping("exam/question/current.json") //加不加@RequestBody 都可以
    public String askCurrentQuestion(@RequestBody Question que) {
        Question question = examService.getExamQuestionByClassify(que.getClassify() , que.getCurrentNumber());
        return gson.toJson(question);
    }

    @ResponseBody
    @RequestMapping("exam/add.json") //加不加@RequestBody 都可以
    public int addExamination(@RequestBody Examination examination) {
        return examService.addExamQuestion(examination);
    }

    @ResponseBody
    @RequestMapping("result/submit.json") //加不加@RequestBody 都可以
    public void submitResult(@RequestBody Result result) {
        resultService.setUserResult(result);
    }

    @ResponseBody //清空classify的数据
    @RequestMapping("result/clear.json") //加不加@RequestBody 都可以
    public void clearResult(@RequestBody Result result) {
        resultService.delUserResultByClassify(result);
    }

    @ResponseBody
    @RequestMapping("result/score.json") //加不加@RequestBody 都可以
    public Score score(@RequestBody Result result) {
        Score score = resultService.computeScore(result);
        return score;
    }
}
